package cn.dao.leave.impl;

import cn.dao.leave.LeaveDao;
import cn.entity.Employee;
import cn.entity.Leave;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Repository("LeaveDao")
public class LeaveDaoImpl extends HibernateDaoSupport implements LeaveDao {
    public LeaveDaoImpl(){
    }
    @Autowired
    public LeaveDaoImpl(@Qualifier("sessionFactory")SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }

    @Override
    public void saveOrUpdate(Leave leave) {
        getHibernateTemplate().saveOrUpdate(leave);
    }

    @Override
    public List<Leave> findBySnAndPage(final Employee employee,final int indexPage,final int pageSize,final Date startDate,final Date endDate) {
        return getHibernateTemplate().execute(new HibernateCallback<List<Leave>>() {
            @Override
            public List<Leave> doInHibernate(Session session) throws HibernateException, SQLException {
                String hql = "from Leave l ";
                if (employee.getSysPosition().getId()==1){
                    hql += "where l.creator.sn=?";
                }else if (employee.getSysPosition().getId()==2){
                    hql += "where l.nextDeal.sn=?";
                }

                if (startDate!=null){
                    hql += " and l.startTime>=:startTime";
                }
                if (endDate!=null){
                    hql += " and l.endTime<=:endTime";
                }
                hql += " order by l.status asc,l.createTime desc";
                Query query = session.createQuery(hql).setString(0,employee.getSn());
                if (startDate!=null){
                    query.setDate("startTime",startDate);
                }
                if (endDate!=null){
                    query.setDate("endTime",endDate);
                }
                int begin = (indexPage-1)*pageSize;
                return query.setFirstResult(begin).setMaxResults(pageSize).list();
            }
        });
    }

    @Override
    public long findCountBySn(final Employee employee,final Date startDate,final Date endDate) {
        return getHibernateTemplate().execute(new HibernateCallback<Long>() {
            @Override
            public Long doInHibernate(Session session) throws HibernateException, SQLException {
                String hql = "select count(l.id) from Leave l ";
                if (employee.getSysPosition().getId()==1){
                    hql += "where l.creator.sn=?";
                }else if (employee.getSysPosition().getId()==2){
                    hql += "where l.nextDeal.sn=?";
                }

                if (startDate!=null){
                    hql += " and l.startTime=:startTime";
                }
                if (endDate!=null){
                    hql += " and l.endTime=:endTime";
                }
                Query query = session.createQuery(hql).setString(0,employee.getSn());
                if (startDate!=null){
                    query.setDate("startTime",startDate);
                }
                if (endDate!=null){
                    query.setDate("endTime",endDate);
                }
                return (Long) query.uniqueResult();
            }
        });
    }

    @Override
    public Leave findById(Long id) {
        return getHibernateTemplate().get(Leave.class,id);
    }
}
